﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RAP.Framework.Libary.Utils
{
    public static class Extentions
    {
        #region 字符串模版转换

        /// <summary>
        /// 将字符串按模版方式转换，模版说明参见TemplateTranslator对象
        /// </summary>
        /// <param name="exp"></param>
        /// <param name="paramObject"></param>
        /// <returns></returns>
        public static string Translator( this string exp, object paramObject, string pattern = null )
        {
            TemplateTranslator translator = new TemplateTranslator( pattern );
            return translator.TranslateTemplate( exp, paramObject );
        }

        public static string Translator( this string exp, Func<string, object> getValueFun, string pattern = null )
        {
            TemplateTranslator translator = new TemplateTranslator( pattern );
            return translator.TranslateTemplate( exp, getValueFun );
        }

        /// <summary>
        /// 用于字符串格式化时的默认参数对象
        /// </summary>
        public static IDictionary STRING_TRANSLATOR_DEFAULT_PARAMS = null;

        /// <summary>
        /// 使用默认的参数对象进行字符串的格式化
        /// </summary>
        /// <param name="exp"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string Translator( this string exp )
        {
            if ( exp.IsNullOrEmpty( ) || STRING_TRANSLATOR_DEFAULT_PARAMS.IsNullOrEmpty( ) )
            {
                return exp;
            }
            return Translator( exp, ( object )STRING_TRANSLATOR_DEFAULT_PARAMS );
        }

        #endregion 字符串模版转换
    }
}
